import { getExam } from '@/utils/exam'

const state = {
  exams: [
    // {
    //   exam: [1, 2],
    //   answer: 10
    // }
  ],
  max: 100, // 最大难度
  total: 10, // 题目个数
  type: 'ADD', // ADD, SUBTRACTION, multiply, MULTIPLY, DIVIDE
  types: ['ADD', 'SUBTRACTION', 'MULTIPLY', 'DIVIDE'],
  typeText: {
    ADD: '+',
    SUBTRACTION: '-',
    MULTIPLY: '×',
    DIVIDE: '÷'
  },
  score: '',
  scores: [],
  showRes: false,
  historyErr: []
}

const mutations = {
  SET_EXAM: (state, payload) => {
    Object.entries(payload).forEach(([key, value]) => {
      state[key] = value
    })
  }
}

const actions = {
  refreshExam: ({ commit, state }) => {
    const { type, max, total } = state
    const examsData = getExam({
      type,
      max,
      total
    })
    const exams = examsData.map(item => {
      return {
        exam: item,
        answer: ''
      }
    })
    commit('SET_EXAM', {
      exams,
      showRes: false
    })
  }
}

export default {
  state,
  mutations,
  actions
}

